package com.naiterui.ehp.bs.pharmacist.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.naiterui.common.redis.RedisUtil;
import com.naiterui.ehp.bp.bo.cms.SysUserBO;
import com.naiterui.ehp.bp.constants.CommonConstant;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.pharmacist.entity.Pharmacist;
import com.naiterui.ehp.bs.pharmacist.exception.ExceptionCodes;
import com.naiterui.ehp.bs.pharmacist.feign.CmsFeignClient;
import com.naiterui.ehp.bs.pharmacist.repository.mysql.PharmacistRepository;
import com.naiterui.ehp.bs.pharmacist.service.IPharmacistCmsService;
import com.naiterui.ehp.bs.pharmacist.vo.user.QrcodeInfoVO;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 药师绑定cms
 *
 * @author yangfuya
 * @version 1.0
 * @date 2021/5/18 11:45
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class PharmacistCmsServiceImpl implements IPharmacistCmsService {

    private final PharmacistRepository pharmacistRepository;
    private final CmsFeignClient cmsFeignClient;

    @Override
    public QrcodeInfoVO qrcodeInfo(Long pharmacistId, Long cmsId, String uuid) throws BusinessException {
        // cms是否已绑定药师
        Long bindPharmacistId = getBindPharmacistId(cmsId);
        if (bindPharmacistId != null) {
            throw new BusinessException(ExceptionCodes.CMS_PHARMACIST_BOUND);
        }
        // 判断二维码是否失效
        String value = RedisUtil.valueOps().getString(CommonConstant.IDENTITY_KEY_REDISKEY + uuid);
        if (StringUtils.isBlank(value)) {
            throw new BusinessException(ExceptionCodes.PHARMACIST_QRCODE_IS_EXPIRED);
        }
        // 判断当前药师是否已经认证通过
        Pharmacist pharmacist = pharmacistRepository.get(pharmacistId);
        if (!Pharmacist.AUTH_STATUS_PASS.equals(pharmacist.getAuthStatus())) {
            throw new BusinessException(ExceptionCodes.PHARMACIST_UNNAMED);
        }
        SysUserBO sysUser = this.cmsFeignClient.info(cmsId);
        String username = sysUser.getUsername();
        String name = sysUser.getName();
        // 是否绑定过其他药师
        Long bound = (Long)RedisUtil.valueOps().getObject(CommonConstant.IDENTITY_PH_REDISKEY + pharmacistId);
        String content;
        if (bound != null) {
            SysUserBO oldUser = this.cmsFeignClient.info(Long.valueOf(bound));
            content = "您已与（" + oldUser.getUsername() + "、" + oldUser.getName() + "）进行绑定，确认后您将与原账号绑定并与PC端账号"
                + "<font color='#1C86E8'>（" + username + "、" + name + "）</font>进行绑定。";
        } else {
            content = "确认后您将与PC端账号<font color='#1C86E8'>（" + username + "、" + name + "）</font>进行绑定。";
        }
        return QrcodeInfoVO.builder().content(content).cmsId(cmsId).build();
    }

    private Long getBindPharmacistId(Long userId) {
        return (Long)RedisUtil.valueOps().getObject(CommonConstant.IDENTITY_REDISKEY + userId);
    }

    @Override
    public void bind(Long pharmacistId, Long cmsId) throws BusinessException {
        // cms是否已绑定药师
        Long bindPharmacistId = getBindPharmacistId(cmsId);
        if (bindPharmacistId != null) {
            throw new BusinessException(ExceptionCodes.CMS_PHARMACIST_BOUND);
        }
        // 是否绑定过其他cms
        Long bound = (Long)RedisUtil.valueOps().getObject(CommonConstant.IDENTITY_PH_REDISKEY + pharmacistId);
        if (bound != null) {
            RedisUtil.keyOps().delete(CommonConstant.IDENTITY_REDISKEY + bound);
            RedisUtil.keyOps().delete(CommonConstant.IDENTITY_PH_REDISKEY + pharmacistId);
        }
        Pharmacist pharmacist = pharmacistRepository.get(pharmacistId);
        if (!Pharmacist.AUTH_STATUS_PASS.equals(pharmacist.getAuthStatus())) {
            throw new BusinessException(ExceptionCodes.PHARMACIST_UNNAMED);
        }
        RedisUtil.valueOps().setObject(CommonConstant.IDENTITY_REDISKEY + cmsId, pharmacistId);
        RedisUtil.valueOps().setObject(CommonConstant.IDENTITY_PH_REDISKEY + pharmacistId, cmsId);
    }

    @Override
    public void untie(Long pharmacistId) throws BusinessException {
        Long cmsId = (Long)RedisUtil.valueOps().getObject(CommonConstant.IDENTITY_PH_REDISKEY + pharmacistId);
        if (cmsId == null) {
            throw new BusinessException(ExceptionCodes.PHARMACIST_UNBOUND);
        }
        RedisUtil.keyOps().delete(CommonConstant.IDENTITY_REDISKEY + cmsId);
        RedisUtil.keyOps().delete(CommonConstant.IDENTITY_PH_REDISKEY + pharmacistId);
    }
}
